
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <math.h>

#include <evhttp.h>

#include "log.h"
#include "util.h"

#include "transfer.h"
#include "evcpe.h"

/* get download data from output_buffer according transfer argument
 */
int evcpe_transfer_get_data(struct evbuffer *output_buffer, 
		struct evcpe_download *download)
{
	int rc;
	unsigned char *data = EVBUFFER_DATA(output_buffer);
	int len = EVBUFFER_LENGTH(output_buffer);
 	int file_size = download->FileSize;

evcpe_info(__func__, "Download content:\n%.*s", len, data);

evcpe_info(__func__, 
		"%d\nDownload Request Content:"
		"\n  CommandKey: %s"
		"\n  FileType: %s"
		"\n  URL: %s"
		"\n  Username: %s"
		"\n  Password: %s"
		"\n  FileSize: %d"
		"\n  TargetFileName: %s"
		"\n  DelaySeconds: %d"
		"\n  SuccessURL: %s"
		"\n  FailureURL: %s", 
		__LINE__, 
		download->CommandKey,
		download->FileType,
		download->URL,
		download->Username,
		download->Password,
		download->FileSize,
		download->TargetFileName,
		download->DelaySeconds,
		download->SuccessURL,
		download->FailureURL);

	if ((file_size) && (len != file_size))
		evcpe_info(__func__, "%d - recieve len (%d) not equal to "
			"request file size (%d)", __LINE__, len, file_size);

	// TODO: get download data by transfer argument
	if(!strncmp(download->FileType, "1 Firmware Upgrade Image", 
			sizeof("1 Firmware Upgrade Image"))) {
		/*deal with Firmware Upgrade Image*/
		evcpe_info(__func__, "deal with Firmware Upgrade Image");
	} else if(!strncmp(download->FileType, "2 Web Content", 
			sizeof("2 Web Content"))) {
		/*deal with Web Content*/
		evcpe_info(__func__, "deal with Web Content");
	} else if(!strncmp(download->FileType, "3 Vendor Configuration File", 
			sizeof("3 Vendor Configuration File"))) {
		/*deal with Vendor Configuration File*/
		evcpe_info(__func__, "deal with Vendor Configuration File");
	} else {
		// TODO: deal with vendor-specific file
		/* The following format is defined to allow the unique definition of 
		 * vendor-specific file "X <OUI> <Vendor-specific identifier>"
		 */
		evcpe_info(__func__, "%d", __LINE__);
	}
evcpe_info(__func__, "%d", __LINE__);
	rc = 0;
	
	return rc;
}

/* add upload data to output_buffer according transfer argument
 */
int evcpe_transfer_add_data(struct evbuffer *output_buffer, 
		struct evcpe_upload *upload)
{
	int rc;
	unsigned char *data = EVBUFFER_DATA(output_buffer);
	int len = EVBUFFER_LENGTH(output_buffer);
 	int file_size = upload->FileSize;

evcpe_info(__func__, "Upload content:\n%.*s", len, data);

evcpe_info(__func__, 
		"%d\nUpload Request Content:"
		"\n  CommandKey: %s"
		"\n  FileType: %s"
		"\n  URL: %s"
		"\n  Username: %s"
		"\n  Password: %s"
		"\n  FileSize: %d"
		"\n  TargetFileName: %s"
		"\n  DelaySeconds: %d"
		"\n  SuccessURL: %s"
		"\n  FailureURL: %s", 
		__LINE__, 
		upload->CommandKey,
		upload->FileType,
		upload->URL,
		upload->Username,
		upload->Password,
		upload->FileSize,
		upload->TargetFileName,
		upload->DelaySeconds,
		upload->SuccessURL,
		upload->FailureURL);

	if ((file_size) && (len != file_size))
		evcpe_info(__func__, "%d - recieve len (%d) not equal to "
			"request file size (%d)", __LINE__, len, file_size);

	// TODO: add upload data by transfer argument

	evbuffer_add(output_buffer, "baby coming", sizeof("baby coming"));
	
	if(!strncmp(upload->FileType, "1 Firmware Upgrade Image", 
			sizeof("1 Firmware Upgrade Image"))) {
		/*deal with Firmware Upgrade Image*/
		evcpe_info(__func__, "deal with Firmware Upgrade Image");
	} else if(!strncmp(upload->FileType, "2 Web Content", 
			sizeof("2 Web Content"))) {
		/*deal with Web Content*/
		evcpe_info(__func__, "deal with Web Content");
	} else if(!strncmp(upload->FileType, "3 Vendor Configuration File", 
			sizeof("3 Vendor Configuration File"))) {
		/*deal with Vendor Configuration File*/
		evcpe_info(__func__, "deal with Vendor Configuration File");
	} else {
		// TODO: deal with vendor-specific file
		/* The following format is defined to allow the unique definition of 
		 * vendor-specific file "X <OUI> <Vendor-specific identifier>"
		 */
		evcpe_info(__func__, "%d", __LINE__);
	}
evcpe_info(__func__, "%d", __LINE__);
	rc = 0;
	
	return rc;
}

